package com.rule.easyrule;

import com.rule.easyrule.config.BizRulesConfig;
import com.rule.easyrule.config.DiscountOrderRule;
import com.rule.easyrule.config.OrdinaryOrderRule;
import com.rule.easyrule.entity.BizOrder;
import com.rule.easyrule.service.BizRuleService;
import com.rule.easyrule.service.OrderService;
import lombok.SneakyThrows;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.support.composite.ActivationRuleGroup;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@EnableAutoConfiguration
@ComponentScan("com.rule.easyrule.service;com.rule.easyrule.mapper;com.rule.easyrule.config")
public class EasyruleApplication {
    @Autowired
    private BizRuleService bizRuleService;
    @Autowired
    private BizRulesConfig bizRulesConfig;

    final String RULE_NAME ="审核规则";
    public static void main(String[] args) {
        try{//这里捕获异常 为了 方便查看 启动失败的原因
            SpringApplication.run(EasyruleApplication.class, args);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping("/")
    String home(){
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        System.out.println( Arrays.asList(beanDefinitionNames));
        return "hello world";
    }
    @PostMapping("/annotation")
    @SneakyThrows
    public void annotationTest(@RequestBody BizOrder order) {
        //定义数据
        Facts facts = new Facts();
        facts.put("order", order);
        ActivationRuleGroup activationRuleGroup = new ActivationRuleGroup("order_type_rule", "订单类型规则");
        activationRuleGroup.addRule(new OrdinaryOrderRule());
        activationRuleGroup.addRule(new DiscountOrderRule());
        //注册
        Rules rules = new Rules();
        rules.register(activationRuleGroup);
        //启动点火
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }
    @PostMapping("/annotationdb")
    @SneakyThrows
    public void annotationdbTest(@RequestBody BizOrder order) {
        //定义数据
        Facts facts = new Facts();
        facts.put("order", order);
        //调用到的方法
        facts.put("OrderService", OrderService.class);
        //读取配置
        Rules rules = bizRulesConfig.fetchConfigRules(RULE_NAME);

        //启动点火
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }

}
